<script setup lang="ts">
import { ref, computed } from "vue";
import { useColumns } from "./columns";
import "plus-pro-components/es/components/search/style/css";
import Plus from "~icons/ep/plus";
import { addNews, setNews, getNews } from "@/api/news";
import { addNotice, getNoticeInfo } from "@/api/common";

defineOptions({
  name: "OperateNotice"
});
import {
  type PlusColumn,
  type FieldValues,
  PlusDialogForm,
  PlusSearch
} from "plus-pro-components";
import { ElMessage } from "element-plus";
import { useMenu } from "@/utils/power";
const power = useMenu(["add", "view"]);

const tableRef = ref();
const defaultSearchState = ref({});

const searchColumns: PlusColumn[] = [
  {
    label: "推送类型",
    prop: "toType",
    valueType: "select",
    options: [
      {
        label: "公告",
        value: 1,
        color: "red"
      },
      {
        label: "私信",
        value: 2,
        color: "red"
      },
      {
        label: "管理员",
        value: 3,
        color: "red"
      }
    ]
  }
];

const rules = {
  title: [
    {
      required: true,
      message: "标题必填"
    }
  ],
  content: [
    {
      required: true,
      message: "内容必填"
    }
  ]
};

const edit = ref(false);
const confirmLoading = ref(false);
const visible = ref(false);
const values = ref<FieldValues>({});
const formRef = ref();
const formColumns: PlusColumn[] = [
  {
    label: "标题",
    prop: "title",
    valueType: "input",
    tooltip: "请输入公告标题"
  },
  {
    label: "公告内容",
    prop: "content",
    valueType: "textarea",
    tooltip: "请输入公告内容"
  }
];

const {
  loading,
  columns,
  dataList,
  pagination,
  loadingConfig,
  adaptiveConfig,
  loadData,
  onSizeChange,
  onKeywordChange,
  onCurrentChange
} = useColumns();

const columnsRef = computed(() => {
  if (power.value.view) {
    return columns;
  }
  const columnsCopy = [...columns];
  return columnsCopy.slice(0, -1);
});

const handleSearch = (value: any) => {
  console.log(value, "search");
  onKeywordChange(value);
};
const handleReset = value => {
  console.log("reset", value);
  onKeywordChange(value);
};

const handleChange = (value: any, target: any) => {
  console.log("change", value);
  onKeywordChange(value);
};

const editClick = async row => {
  console.log(
    "%crow===详情>>>: ",
    "color: MidnightBlue; background: Aquamarine; font-size: 20px;",
    row
  );
  try {
    const res = await getNoticeInfo(row.noticeId);
    const data = res.data;
    values.value = data;
    edit.value = true;
    visible.value = true;
    // setTimeout(() => {
    //   editorRef.value?.dangerouslyInsertHtml(data.content);
    // }, 100);
    // editorRef.value?.dangerouslyInsertHtml(data.content);
  } catch (error) {
    console.error(error);
  }
};
const add = () => {
  edit.value = false;
  values.value = { content: "", toType: 1, title: "" };
  visible.value = true;
};
const handleSubmit = async (value: FieldValues) => {
  if (edit.value) {
    ElMessage.error("推送不可编辑");
  } else {
    const updatedValues = { ...values.value };

    confirmLoading.value = true;
    try {
      await addNotice(updatedValues);
      visible.value = false;
      confirmLoading.value = false;
      formRef.value?.formInstance?.resetFields();
      loadData();
    } catch (error) {
      confirmLoading.value = false;
      console.log(error, "error");
    }
  }
};
const handleCancel = () => {
  console.log("取消");
};
</script>

<template>
  <ElCard>
    <template #header>
      <ElPageHeader :icon="null">
        <template #title>
          <h3>推送管理</h3>
        </template>
        <template v-if="power.add" #extra>
          <div class="flex flex-row items-end justify-end">
            <el-button type="primary" @click="add">
              <el-icon :size="15" color="#fff">
                <IconifyIconOffline :icon="Plus" />
              </el-icon>
              添加推送
            </el-button>
          </div>
        </template>
        <PlusSearch
          :defaultValues="defaultSearchState"
          class="mt-5"
          :columns="searchColumns"
          @search="handleSearch"
          @change="handleChange"
          @reset="handleReset"
        />
      </ElPageHeader>
    </template>
    <pure-table
      ref="tableRef"
      showOverflowTooltip
      border
      adaptive
      :fit="true"
      :adaptiveConfig="adaptiveConfig"
      row-key="id"
      alignWhole="center"
      :loading="loading"
      :loading-config="loadingConfig"
      :data="dataList"
      :columns="columnsRef"
      :pagination="pagination"
      @page-size-change="onSizeChange"
      @page-current-change="onCurrentChange"
    >
      <template #operation="{ row }">
        <el-button
          v-if="power.view"
          type="primary"
          size="small"
          @click="editClick(row)"
        >
          查看
        </el-button>
      </template>

      <template #banner="{ row, index }">
        <el-image
          preview-teleported
          loading="lazy"
          :src="row.banner"
          :preview-src-list="dataList.map(v => v.banner)"
          :initial-index="index"
          fit="cover"
          class="w-[50px] h-[50px]"
        />
      </template>
    </pure-table>
    <PlusDialogForm
      ref="formRef"
      v-model:visible="visible"
      v-model="values"
      :form="{ columns: formColumns, rules, labelWidth: 120 }"
      :dialog="{ confirmLoading }"
      :title="edit ? '查看推送' : '添加推送'"
      @confirm="handleSubmit"
      @cancel="handleCancel"
    />
  </ElCard>
</template>

<style scoped>
:deep(.el-descriptions),
:deep(.el-descriptions__table tr) {
  --el-card-bg-color: transparent !important;
  --el-fill-color-blank: transparent !important;
  background: transparent !important;
}
.wrap-text {
  white-space: normal !important; /* 允许自动换行 */
  word-break: break-all; /* 强制换行 */
}
:deep(video) {
  width: 100% !important;
}
</style>
